package com.Register.config;

import com.Register.properties.ElasticSearchProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
es配置类
 */
@Configuration
@Slf4j
public class ElasticSearchConfig {
    @Bean(destroyMethod = "close") //程序开始时交给bean对象注入, 指定了当bean被销毁时应该调用其close方法
    @ConditionalOnMissingBean//保证spring容器里面只有一个utils对象(当没有这个bean对象再去创建，有就没必要去创建了)
    public RestHighLevelClient client(ElasticSearchProperties elasticSearchProperties){
        log.info("读取到的es服务器主机号为:{}",elasticSearchProperties.getHost());
        log.info("读取到的es服务器主机端口号为:{}",elasticSearchProperties.getPort());
        return new RestHighLevelClient(RestClient.builder(
                new HttpHost(
                        elasticSearchProperties.getHost(),
                        Integer.parseInt(elasticSearchProperties.getPort()),
                        "http"
                )
        ));
    }

}
